home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / JOYSTICK.SWG / 0003_JOYSTCK3.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  5KB  |  204 lines

  1. {
  2. to whomever sent me a message concerning joystick support, I apologize that I
  3. cannot send this message to you directly (message Pointers were screwed up on m
  4. end, and I lost your message), but here is both my source For a Unit and a
  5. sample Program.  First I'd like to say that my Unit may be somewhat inComplete.
  6. have only a Single joystick port, so reading of two ports is impossible.  For
  7. this reason, I'd like to ask any and all to make suggestions, and modifications
  8. so that I, and all Programmers, may have a Complete Unit.  Also, remarks have
  9. not been added to the Program, if an explanation is needed, please feel free to
  10. ask...I'd be more than happy to give explanations For my work.  Anyhows, here i
  11. is...
  12. }
  13. Unit Joystick;
  14.  
  15. Interface
  16.  
  17. Function JoystickExists : Boolean;
  18. Function JoystickPosX : Integer;
  19. Function JoystickPosY : Integer;
  20. Function JoystickButtonA : Boolean;
  21. Function JoystickButtonB : Boolean;
  22.  
  23. Implementation
  24.  
  25. Uses Crt, Dos;
  26.  
  27. Const GamePortAddr = $200;
  28.      MaxCount = 500;
  29.  
  30. Function JoystickStatus (Mask : Byte) : Integer;
  31. Var Counter : Integer;
  32. Label Read;
  33. begin
  34.   Asm
  35.   mov cx,MaxCount
  36.   mov dx,GamePortAddr
  37.   mov ah,Mask
  38.   out dx,al
  39.   read:
  40.      in al,dx
  41.      test al,ah
  42.      loopnz read
  43.   mov counter,cx
  44.   end;
  45.   JoystickStatus := MaxCount - Counter;
  46.   Delay (2);
  47. end;
  48.  
  49. Function JoystickPosX : Integer;
  50. begin
  51.   JoystickPosX := JoystickStatus (1);
  52. end;
  53.  
  54. Function JoystickPosY : Integer;
  55. begin
  56.   JoystickPosY := JoystickStatus (2);
  57. end;
  58.  
  59. Function JoystickButtonA : Boolean;
  60. begin
  61.   JoystickButtonA := (Port [GamePortAddr] and 16) = 0;
  62. end;
  63.  
  64. Function JoystickButtonB : Boolean;
  65. begin
  66.   JoystickButtonB := (Port [GamePortAddr] and 32) = 0;
  67. end;
  68.  
  69. Function JoystickExists : Boolean;
  70. Var Regs : Registers;
  71. begin
  72.   JoystickExists := not ((JoystickPosX = 0) and (JoystickPosY = 0));
  73. end;
  74.  
  75. end.
  76.  
  77.  
  78. Program JoyTest;
  79.  
  80. Uses Crt, Dos, AniVGA, Joystick;
  81.  
  82. Var XMin, XMax, YMin, YMax,
  83.    XRange, YRange,
  84.    X, Y,
  85.    PosX, PosY,
  86.    Bullet1X, Bullet1Y,
  87.    Bullet2X, Bullet2Y : Integer;
  88.    Shooting1, Shooting2 : Boolean;
  89.    ShootNext : Boolean;
  90.  
  91. Procedure CalibrateJoystick (Var XMin, XMax, YMin, YMax : Integer);
  92. begin
  93.   Write ('Press joystick to upper left corner and press button one...');
  94.   Repeat Until JoystickButtonA;
  95.   XMin := JoystickPosX;
  96.   YMin := JoystickPosY;
  97.   Writeln ('OK.');
  98.   Repeat Until not JoystickButtonA;
  99.   Write ('Press joystick to lower right corner and press button two...');
  100.   Repeat Until JoystickButtonB;
  101.   XMax := JoystickPosX;
  102.   YMax := JoystickPosY;
  103.   Writeln ('OK.');
  104.   Repeat Until not JoystickButtonB;
  105. end;
  106.  
  107. Procedure AnimateShip;
  108. begin
  109.   X := JoystickPosX - XMin;
  110.   if (X <= XRange div 3) then
  111.      Dec (PosX, 3)
  112.   else if (X > XRange * 2 div 3) then
  113.      Inc (PosX, 3);
  114.   Y := JoystickPosY - YMin;
  115.   if (Y <= YRange div 3) then
  116.      Dec (PosY, 3)
  117.   else if (Y > YRange * 2 div 3) then
  118.      Inc (PosY, 3);
  119.   SpriteX [0] := PosX;
  120.   SpriteY [0] := PosY;
  121. end;
  122.  
  123. Procedure AnimateBullets;
  124. begin
  125.   if Shooting1 then
  126.      if (Bullet1Y < 0) then
  127.         Shooting1 := False
  128.      else
  129.         Dec (Bullet1Y, 8)
  130.   else
  131.      begin
  132.         Bullet1X := PosX + 3;
  133.         Bullet1Y := PosY + 14;
  134.      end;
  135.   if Shooting2 then
  136.      if (Bullet2Y < 0) then
  137.         Shooting2 := False
  138.     else
  139.         Dec (Bullet2Y, 8)
  140.   else
  141.      begin
  142.         Bullet2X := PosX + 30;
  143.         Bullet2Y := PosY + 14;
  144.      end;
  145.   SpriteX [1] := Bullet1X;
  146.   SpriteY [1] := Bullet1Y;
  147.   SpriteX [2] := Bullet2X;
  148.   SpriteY [2] := Bullet2Y;
  149. end;
  150.  
  151. begin
  152.   if JoystickExists and (LoadSprite ('SHIP1.COD', 1) = 1) and
  153.      (LoadSprite ('BULLET.COD', 2) = 1) then
  154.      begin
  155.         ClrScr;
  156.         CalibrateJoystick (XMin, XMax, YMin, YMax);
  157.         ClrScr;
  158.         InitGraph;
  159.         SpriteN [0] := 1;
  160.         SpriteN [1] := 2;
  161.         SpriteN [2] := 2;
  162.         PosX := 160;
  163.         PosY := 160;
  164.         Shooting1 := False;
  165.         XRange := XMax - XMin;
  166.         YRange := YMax - YMin;
  167.         ShootNext := Boolean (0);
  168.         While not (JoystickButtonA and JoystickButtonB) do
  169.            begin
  170.               if JoystickButtonA and not JoystickButtonB then
  171.                  if not Shooting1 and ShootNext then
  172.                     begin
  173.                        Bullet1X := PosX + 3;
  174.                        Bullet1Y := PosY + 14;
  175.                        Shooting1 := True;
  176.                        ShootNext := False;
  177.                     end
  178.                  else if not Shooting2 and not ShootNext then
  179.                     begin
  180.                        Bullet2X := PosX + 30;
  181.                        Bullet2Y := PosY + 14;
  182.                        Shooting2 := True;
  183.                        ShootNext := True;
  184.                     end;
  185.               While JoystickButtonA do
  186.                  begin
  187.                     AnimateShip;
  188.                     AnimateBullets;
  189.                     Animate;
  190.                  end;
  191.               AnimateShip;
  192.               AnimateBullets;
  193.               Animate;
  194.            end;
  195.         CloseRoutines;
  196.      end
  197.   else
  198.      Writeln ('Game card not installed.');
  199. end.
  200. {
  201. I apologize For giving you an example that Uses another Unit.  if need be, this
  202. Program can be easily modified to provide a successful example.  Hope this
  203. helps, and I hope my Programming is not toO bad.
  204. }